﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace LevelEditor.Forms
{
    public partial class LayerDetailsControl : UserControl
    {
        #region Fields

        LayerDialog dialog;
        
        bool dontDirty;

        #endregion

        #region Properties

        public int LayerIndex { get; set; }

        public int LayerWidth { get { return Convert.ToInt32(widthNUD.Value); } }

        public int LayerHeight { get { return Convert.ToInt32(heightNUD.Value); } }

        #endregion

        #region Constructors

        public LayerDetailsControl(MainForm form, int index)
        {
            InitializeComponent();
            LayerIndex = index;
            dialog = new LayerDialog(form.Level.Layers[LayerIndex]);
            dontDirty = false;
            ResetControls(form);
        }

        #endregion

        #region Methods

        public void ResetControls(MainForm form)
        {
            if (form.CurrentLayer == LayerIndex)
                BackColor = SystemColors.ActiveCaption;
            else
                BackColor = SystemColors.Control;
            if (form.Level.Layers[LayerIndex].Visible)
                visibleCB.BackgroundImage = LevelEditor.Properties.Resources.Visible;
            else
                visibleCB.BackgroundImage = null;
            if (palettePB.Image != null)
                palettePB.Image.Dispose();
            palettePB.Image = new Bitmap(form.LayerViewControl.Views[LayerIndex].TileSetImages[0]);
            dontDirty = true;
            widthNUD.Value = form.Level.Layers[LayerIndex].Size.Width;
            heightNUD.Value = form.Level.Layers[LayerIndex].Size.Height;
            dontDirty = false;
            Invalidate();
        }

        #endregion

        #region Methods

        public void UpdateVisibleImage(MainForm form)
        {
            if (form.Level.Layers[LayerIndex].Visible)
                visibleCB.BackgroundImage = LevelEditor.Properties.Resources.Visible;
            else
                visibleCB.BackgroundImage = null;
            visibleCB.Invalidate();
        }

        #endregion

        #region EventHandlers

        public delegate void MoveUpClickedEventHandler(object sender, EventArgs e);
        public event MoveUpClickedEventHandler MoveUpClicked;
        protected void OnMoveUpClicked(EventArgs e)
        {
            if (MoveUpClicked != null)
                MoveUpClicked(this, e);
        }

        public delegate void MoveDownClickedEventHandler(object sender, EventArgs e);
        public event MoveDownClickedEventHandler MoveDownClicked;
        protected void OnMoveDownClicked(EventArgs e)
        {
            if (MoveDownClicked != null)
                MoveDownClicked(this, e);
        }

        public delegate void WidthChangedEventHandler(object sender, EventArgs e);
        public event WidthChangedEventHandler WidthChanged;
        protected void OnWidthChanged(EventArgs e)
        {
            if (WidthChanged != null)
                WidthChanged(this, e);
        }

        public delegate void HeightChangedEventHandler(object sender, EventArgs e);
        public event HeightChangedEventHandler HeightChanged;
        protected void OnHeightChanged(EventArgs e)
        {
            if (HeightChanged != null)
                HeightChanged(this, e);
        }

        public delegate void OptionsClickedEventHandler(object sender, EventArgs e);
        public event OptionsClickedEventHandler OptionsClicked;
        protected void OnOptionsClicked(EventArgs e)
        {
            if (OptionsClicked != null)
                OptionsClicked(this, e);
        }

        public delegate void VisibleClickedEventHandler(object sender, EventArgs e);
        public event VisibleClickedEventHandler VisibleClicked;
        protected void OnVisibleClicked(EventArgs e)
        {
            if (VisibleClicked != null)
                VisibleClicked(this, e);
        }

        public delegate void PaletteClickedEventHandler(object sender, EventArgs e);
        public event PaletteClickedEventHandler PaletteClicked;
        protected void OnPaletteClicked(EventArgs e)
        {
            if (PaletteClicked != null)
                PaletteClicked(this, e);
        }

        #endregion

        #region Events

        private void moveUpButton_Click(object sender, EventArgs e)
        {
            OnMoveUpClicked(e);
        }

        private void moveDownButton_Click(object sender, EventArgs e)
        {
            OnMoveDownClicked(e);
        }

        private void widthNUD_ValueChanged(object sender, EventArgs e)
        {
            if (!dontDirty)
            {
                OnWidthChanged(e);
            }
        }

        private void heightNUD_ValueChanged(object sender, EventArgs e)
        {
            if (!dontDirty)
            {
                OnHeightChanged(e);
            }
        }

        private void optionsButton_Click(object sender, EventArgs e)
        {
            OnOptionsClicked(e);
        }

        private void visibleCB_Click(object sender, EventArgs e)
        {
            OnVisibleClicked(e);
        }

        private void palettePB_Click(object sender, EventArgs e)
        {
            OnPaletteClicked(e);
        }

        private void LayerDetailsControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                palettePB_Click(sender, e);
        }

        #endregion
    }
}
